﻿using ERPPublic;
using FinancialManagement.Application.Command.SalesReturnCommand;
using FinancialManagement.Application.Command.SalesShouldRefundCommand;
using FinancialManagement.Domain.Dto;
using FinancialManagement.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FinancialManagement.Controllers
{
    /// <summary>
    /// 销售退货/应退销售款
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class SalesReturnController : ControllerBase
    {
        protected readonly IMediator mediatorp;
        public SalesReturnController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }


        /// <summary>
        /// 显示销售退货
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<ListSalesReturnDto>>> GetSalesReturn([FromQuery] ListSalesReturnCommand command)
        {
            ApiResult<PageDto<ListSalesReturnDto>> result = new ApiResult<PageDto<ListSalesReturnDto>>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 销售退货状态修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdStateSalesReturn([FromQuery] StateUpdSalesReturnCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 应退销售款列表显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<ListSalesReturnDto>>> GetSalesShouldRefundList([FromQuery] ListSalesShouldRefundCommand command)
        {
            ApiResult<PageDto<ListSalesReturnDto>> result = new ApiResult<PageDto<ListSalesReturnDto>>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 应退销售款(直接退款)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<SalesDirectDebitDto>> GetSalesDirectDebitList([FromQuery] SalesDirectDebitCommand command)
        {
            ApiResult<SalesDirectDebitDto> result = new ApiResult<SalesDirectDebitDto>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 应退销售款(直接退款)添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddSalesDirectDebit(AddSalesShouldRefundCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }



    }
}
